﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NCalc;
using System.Text.RegularExpressions;
using System.Globalization;

namespace Citi.Scheduler.Domain
{
    public partial class RoutineParameter
    {
        public Dictionary<string, object> Variables = new Dictionary<string, object>
            {
                { "[Date]", DateTime.Today.ToString("dd/MM/yyyy") },
                { "[Year]", DateTime.Today.ToString("yyyy") },
                { "[Month]", DateTime.Today.ToString("MM") },
                { "[Day]", DateTime.Today.ToString("dd") },
                { "[Now]", DateTime.Now },
                { "[Month-MMM-PT]", DateTime.Today.ToString("MMM", new CultureInfo("pt-BR")) },
                { "[Month-MMM-PT-upper]", DateTime.Today.ToString("MMM", new CultureInfo("pt-BR")).ToUpper() },
                { "[Month-MMMM-PT]", DateTime.Today.ToString("MMMM", new CultureInfo("pt-BR")) },
                { "[Month-MMMM-PT-upper]", DateTime.Today.ToString("MMMM", new CultureInfo("pt-BR")).ToUpper() },
                { "[Month-MMM-EN]", DateTime.Today.ToString("MMM", new CultureInfo("en-US")) },
                { "[Month-MMM-EN-upper]", DateTime.Today.ToString("MMM", new CultureInfo("en-US")).ToUpper() },
                { "[Month-MMMM-EN]", DateTime.Today.ToString("MMMM", new CultureInfo("en-US")) },
                { "[Month-MMMM-EN-upper]", DateTime.Today.ToString("MMMM", new CultureInfo("en-US")).ToUpper() },
                { "[Year-yy]", DateTime.Today.ToString("yy") },
            };

        public static object FillVariables(string value)
        {
            if (value.Contains("[") && value.Split(new[] { '[', ']' }).Count() == 1)
            {
                try
                {
                    var variable = new RoutineParameter().Variables[value];
                    return variable ?? "Variable not found";
                }
                catch
                {

                }
            }

            foreach (var variable in new RoutineParameter().Variables)
                value = value.Replace(variable.Key, variable.Value.ToString());

            return EvaluateMathOperators(value);
        }

        private static string EvaluateMathOperators(string value)
        {
            if (!value.Contains("Math{")) return value;

            Regex.Matches(value, "Math{(.*?)}").Cast<Match>().ToList().ForEach(x =>
            {
                var mathExpression = x.ToString().Replace("{{", "{");

                var expression = new Expression(mathExpression.Replace("Math", string.Empty));

                if (!expression.HasErrors())
                    value = value.Replace(mathExpression, expression.Evaluate().ToString());
                else
                    value = value.Replace(mathExpression, "Math Error");
            });

            return !value.Contains("Math{") ? value : EvaluateMathOperators(value);
        }
    }
}
